﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Windows.Forms.Integration;

namespace comm.editor
{
    public class CreateStateEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            var edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (edSvc != null)
            {
                var elHost = new ElementHost
                {
                    Width = 240,
                    Height = 320,
                    Child = new UCCreateState((List<comm.objst.CreateState>)value)
                };
                edSvc.DropDownControl(elHost);
                return value;
            }
            return value;
        }
    }

    /// <summary>
    /// UCState.xaml 的交互逻辑
    /// </summary>
    public partial class UCCreateState : UserControl
    {
        List<comm.objst.CreateState> _createList = null;

        public UCCreateState(List<comm.objst.CreateState> createList)
        {
            InitializeComponent();

            _createList = createList;

            var states = DefineFile.LastInstance.GetAllState();
            foreach (var stateId in states)
            {
                cbState.Items.Add(stateId);
            }
            if (cbState.Items.Count > 0)
            {
                cbState.SelectedIndex = 0;
            }

            btnAdd.Click += new RoutedEventHandler(btnAdd_Click);
            btnClear.Click += new RoutedEventHandler(btnClear_Click);

            Fill();
        }

        void btnClear_Click(object sender, RoutedEventArgs e)
        {
            _createList.Clear();
            Fill();
        }
        void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            if (cbState.SelectedIndex != -1)
            {
                _createList.Add(new objst.CreateState
                {
                    StateId = cbState.SelectedItem.ToString(),
                    Dur = 1000
                });
                Fill();
            }
        }

        void Fill()
        {
            spList.Children.Clear();

            var list = new List<comm.objst.CreateState>();
            foreach (var item in _createList)
            {
                if (DefineFile.LastInstance.GetState(item.StateId) != null)
                {
                    list.Add(item);
                }
            }
            _createList.Clear();
            foreach (var item in list)
            {
                _createList.Add(item);
            }

            foreach (var item in _createList)
            {
                var item2 = item;
                var grid = new Grid { Margin = new Thickness(0, 0, 0, 4) };

                grid.Children.Add(new TextBlock
                {
                    Width = 80,
                    VerticalAlignment = VerticalAlignment.Center,
                    HorizontalAlignment = HorizontalAlignment.Left,
                    Text = item.StateId
                });

                var tbDur = new TextBox
                {
                    Text = item.Dur.ToString(),
                    VerticalAlignment = VerticalAlignment.Center,
                    Margin = new Thickness(80, 0, 24, 0)
                };
                tbDur.TextChanged += new TextChangedEventHandler((o, e) =>
                {
                    int n = 0;
                    if (int.TryParse(tbDur.Text, out n))
                    {
                        item2.Dur = n;
                        tbDur.Opacity = 1;
                    }
                    else
                    {
                        tbDur.Opacity = .5;
                    }
                });
                grid.Children.Add(tbDur);

                #region btnDel
                var btnDel = new Button
                {
                    Content = "X",
                    Width = 22,
                    Height = 22,
                    VerticalAlignment = VerticalAlignment.Center,
                    HorizontalAlignment = HorizontalAlignment.Right
                };
                btnDel.Click += new RoutedEventHandler((o, e) =>
                {
                    _createList.Remove(item2);
                    Fill();
                });
                grid.Children.Add(btnDel);
                #endregion

                spList.Children.Add(grid);
            }
        }
    }
}
